package com.etime.shycourse.services.coupons.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.services.coupons.interfaces.CouponrangeServices;
import com.etime.shycourse.dao.CouponrangeMapper;
import com.etime.shycourse.dto.CouponrangePackageDto;
import com.etime.shycourse.pojo.Couponrange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 *
 * @author wb
 * @since 2022-03-15
 */
@Service
public class CouponrangeServiceImpl implements CouponrangeServices {
    @Autowired
    private CouponrangeMapper mapper;

    /**
     * 查询优惠券的使用范围 返回第一个使用范围
     *
     * @param CouponId
     * @return Integer
     */
    @Override
    public Couponrange selectCouponRangeById(Integer CouponId) {
        List<Couponrange> list = mapper.selectList (new QueryWrapper<Couponrange> ().eq ("couponid", CouponId));
        if (list.size () > 0) {
            return list.get (0);// 返回所有
        }
        return null; //没有返回空
    }

    @Override
    public List<Couponrange> selectCouponRangeListById(Integer couponId) {
        List<Couponrange> list = mapper.selectList (new QueryWrapper<Couponrange> ().eq ("couponid", couponId));
        if (list.size () > 0) {
            return list;// 返回所有
        }
        return null; //没有返回空
    }

    /**
     * 创建使用优惠劵范围（课程）
     * 如果使用范围存在 则进行删除，再进行创建。
     *
     * @param couponrange
     * @return Integer
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertCouponRange(List<Couponrange> couponrange) {
        if (couponrange.size() == 0) {
            return 0;
        }
        try {
            // 如果使用范围存在 则先进行删除
            if (couponrange.get(0).getCouponid() != null) {
                QueryWrapper<Couponrange> wrapper = new QueryWrapper<>();
                wrapper.eq("couponid", couponrange.get(0).getCouponid());
                int delete = mapper.delete(wrapper);
                // 删除失败
            }
            // 使用范围不存在 则进行添加
            couponrange.forEach (l -> {
                int insert = mapper.insert (l);
                if (insert == 0) {
                    try {
                        throw new Exception ("创建范围失败！");
                    } catch (Exception e) {
                        e.printStackTrace ();
                    }
                }
            });
            return 1;// 成功
        } catch (Exception e) {
            e.printStackTrace ();
            // @Transactional和try catch捕获异常会让注解失效
            TransactionAspectSupport.currentTransactionStatus ().setRollbackOnly ();
            return 0;
        }
    }

    /**
     * 优惠券使用范围（包含课程包）分页
     *
     * @return Page<CouponrangePackageDto>
     */
    @Override
    public Page<CouponrangePackageDto> selectCouponrangePackageDtoPage() {
        return mapper.selectCouponrangePackageDtoPage ();
    }
}
